package com.tjsinfo.tjsoa.Dao;

import java.sql.*;

import javax.sql.DataSource;

import org.springframework.jdbc.core.PreparedStatementCallback;

public class JDBCDao implements IJDBCDao {
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	
	private DataSource dataSource;
	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public Connection getConn() {
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			// TODO �Զ���� catch ��
			e.printStackTrace();
		}
		return conn;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.Dao.IDao#executeUpdate(java.lang.String)
	 */
	public int executeUpdate(String sql) {
		conn = getConn();
		if(conn == null)System.out.println("not null");
		int i = 0;
		try {
			stmt = conn.createStatement();
			i = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO �Զ���� catch ��
			e.printStackTrace();
		}
		return i;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.Dao.IDao#executeQuary(java.lang.String)
	 */
	public ResultSet executeQuary(String sql) {
		conn = getConn();
		
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			// TODO �Զ���� catch ��
			e.printStackTrace();
		}
		return rs;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.Dao.IDao#closeConn()
	 */
	public void closeConn() {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO �Զ���� catch ��
				e.printStackTrace();
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.Dao.IDao#closeStmt()
	 */
	public void closeStmt() {
			if (stmt != null) {
					try {
						stmt.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
	
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.Dao.IDao#closeRs()
	 */
	public void closeRs() {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			// TODO �Զ���� catch ��
			e.printStackTrace();
		}
	}
	
	public void cunchu(CallableStatement cstmt) {
		try {
			cstmt.execute();
			cstmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public CallableStatement returnCstmt(String procedure) {
		conn = getConn();
		CallableStatement cstmt = null;
		try {
			cstmt = conn.prepareCall(procedure);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cstmt;
	}
}
